package com.housekeeping.housekeeping.user.controller;

import com.housekeeping.housekeeping.serviceItem.utils.ResponseUtils;
import com.housekeeping.housekeeping.serviceItem.utils.StatUtil;
import com.housekeeping.housekeeping.user.entity.Account;
import com.housekeeping.housekeeping.user.service.UserService;
import com.housekeeping.housekeeping.user.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户注册
 * @author tomyeung-李尐达
 */
@WebServlet("/User/register")
public class UserRegisterController extends HttpServlet {
    private UserService service=new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String classifyId = req.getParameter("classifyId");
        if (name==null||account==null||password==null||phone==null||classifyId==null) {ResponseUtils.fail(resp, StatUtil.PARAMS_VALID); }
        else {
            try {
                //判断用户是否存在
                if (service.accountExist(account)>0){ResponseUtils.fail(resp,StatUtil.ACCOUNT_EXIST);}
                else {
                    //不存在,允许注册
                    int res= service.register(new Account(name,account,password,phone,Integer.valueOf(classifyId)));
                    //1,注册成功，返回成功响应
                    if (res==1){ResponseUtils.success(resp,StatUtil.SUCCESS);}
                    //其他，失败，返回失败响应
                    else {ResponseUtils.fail(resp,StatUtil.SERVER_BUST);}
                }
            }catch (Exception e)
            {
                ResponseUtils.fail(resp,StatUtil.INTERNET_ERR);
                e.printStackTrace();
            }
        }
    }
}
